package com.qf.commerce.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.commerce.common.dto.DistributorOrderDto;
import com.qf.commerce.common.dto.DistributorOrderSelectDto;
import com.qf.commerce.common.dto.DistributorOrderToolDto;
import com.qf.commerce.common.vo.DistributorOrderVo;
import com.qf.commerce.entity.DistributorOrder;
import com.qf.commerce.exception.ControllerException;
import com.qf.commerce.exception.DaoException;
import com.qf.commerce.exception.ServiceException;
import com.qf.commerce.mapper.DistributorOrderMapper;
import com.qf.commerce.resp.ResponseStatus;
import com.qf.commerce.service.BalanceTransactionsService;
import com.qf.commerce.service.DistributorOrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

/*
 * @author 358
 * @since JDK 1.8
 * @data 2023/9/14 17:16
 */

@Service
public class DistributorOrderServiceImpl implements DistributorOrderService {

    @Resource
    private DistributorOrderMapper distributorOrderMapper;
    @Resource
    private BalanceTransactionsService balanceTransactionsService;

    @Override
    public int addDistributorOrder(DistributorOrderDto dod) {
        DistributorOrder dor = new DistributorOrder();
        if (ObjectUtils.isEmpty(dor) || ObjectUtils.isEmpty(dod)) {
            throw new ServiceException(ResponseStatus.DATA_ERROR);
        }
        BeanUtils.copyProperties(dod, dor);
        DistributorOrderToolDto dp = distributorOrderMapper.selectByOrderId(dod.getOrderId());
        if (dp.getCommissionOne() != null) {
            dor.setCommissionOne(dp.getCommissionOne());
        } else if (dp.getCommissionPercentOne() != null) {
            dor.setCommissionOne(dp.getOrderMoney().multiply(dp.getCommissionPercentOne()));
        } else throw new ServiceException(ResponseStatus.DATA_ERROR);
        if (dod.getDealerTwoId() != null) {
            if (dp.getCommissionTwo() != null) {
                dor.setCommissionTwo(dp.getCommissionTwo());
            } else if (dp.getCommissionPercentTwo() != null) {
                dor.setCommissionTwo(dp.getOrderMoney().multiply(dp.getCommissionPercentTwo()));
            }
        }
        dor.setStatus(1);
        return distributorOrderMapper.insert(dor);
    }

    @Override
    public PageInfo<DistributorOrderVo> list(Integer page, Integer size) {
        return PageHelper.startPage(page, size).doSelectPageInfo(() -> distributorOrderMapper.selectAll());
    }

    @Override
    public PageInfo<DistributorOrderVo> select(DistributorOrderSelectDto dto) {
        return PageHelper.startPage(dto.getPage(), dto.getSize()).doSelectPageInfo(() -> distributorOrderMapper.select(dto));
    }

    @Override
    @Transactional
    public Integer statusSuccess(Integer distributorOrderId) {
        if (distributorOrderId == null) {
            throw new ControllerException(ResponseStatus.DATA_ERROR);
        } else {
            DistributorOrder door = distributorOrderMapper.selectUserIdByDistributorOrderId(distributorOrderId);
            if(door.getStatus()!=1){
                throw new ServiceException(ResponseStatus.DATA_ERROR);
            }
            if (door.getDealerOneId() == null) {
                throw new DaoException(ResponseStatus.GET_DATA_ERROR);
            }
            BigDecimal b1 = balanceTransactionsService.add(door.getDealerOneId(), door.getCommissionOne(), "给予佣金");
            if (b1 == null) {
                throw new DaoException(ResponseStatus.GET_DATA_ERROR);
            }
            if (door.getDealerTwoId() != null) {
                BigDecimal b2 = balanceTransactionsService.add(door.getDealerTwoId(), door.getCommissionTwo(), "给予佣金");
                if (b2 == null) {
                    throw new DaoException(ResponseStatus.GET_DATA_ERROR);
                }
            }
            DistributorOrder dor = new DistributorOrder();
            dor.setDistributorOrderId(distributorOrderId);
            dor.setStatus(2);
            dor.setEndDate(new Date());
            int num = distributorOrderMapper.updateStatusById(dor);
            if (num > 0) {
                return num;
            } else {
                throw new DaoException(ResponseStatus.SYS_ERROR);
            }
        }
    }
}
